/* global Module */

/* Magic Mirror
 * Module: jsoninfo
 *
 * By Michael Teeuw http://michaelteeuw.nl
 * MIT Licensed.
 */
 

Module.register("MMM-DHT11",{

	// Default module config.
	defaults: {
		fadeSpeed:2000,
		sensorPIN: 22,
		updateInterval: 1 * 60 * 1000 // Update every 1 minutes.
	},

	// Override start method.
	start: function() {
		Log.log("Starting module: " + this.name);
		this.jsonData = null;
		this.loaded = false;
		this.getData();
		setInterval(function() {
		   this.getData();
		}, this.config.updateInterval);
	},
	// Define required css.
	getStyles: function() {
		return ["MMM-DHT11.css"];
	},
	// Override required translations.
	getTranslations: function() {
		return {
			zh_cn: "translations/zh_cn.json"
		};
	},
	getData: function(){
		this.sendSocketNotification("GET_DATA", this.config);
	},
	// Override dom generator.
	getDom: function() {				
		var wrapper = document.createElement("table");
		wrapper.className = "small";
		if (this.jsonData===null) {
			wrapper.innerHTML = this.translate("WAITING");
			wrapper.className = "small dimmed";
			return wrapper;
		}
		var itemWrapper = document.createElement("tr");
		itemWrapper.className = "normal";
		var titleWrapper = document.createElement("td");
		titleWrapper.innerHTML = this.translate("temperature");
		titleWrapper.className = "title bright";
		itemWrapper.appendChild(titleWrapper);
		var valueWrapper =  document.createElement("td");
		valueWrapper.innerHTML =this.jsonData.temperature;
		valueWrapper.className = "time light";
		itemWrapper.appendChild(valueWrapper);
		wrapper.appendChild(itemWrapper);
		
		var itemWrapper1 = document.createElement("tr");
		itemWrapper1.className = "normal";
		var titleWrapper1 = document.createElement("td");
		titleWrapper1.innerHTML = this.translate("humidity");
		titleWrapper1.className = "title bright";
		itemWrapper1.appendChild(titleWrapper1);
		var valueWrapper1 =  document.createElement("td");
		valueWrapper1.innerHTML =this.jsonData.humidity;
		valueWrapper1.className = "time light";
		itemWrapper1.appendChild(valueWrapper1);
		
		wrapper.appendChild(itemWrapper1);		
		return wrapper;
	},
	// Subclass socketNotificationReceived received.
	socketNotificationReceived: function(notification, payload) {
		
		if (notification === "UPDATE_DATA") {
			this.jsonData=payload;
			this.loaded = true;
			this.updateDom(this.config.fadeSpeed);
		}
	}
});
